
import com.nttdocomo.ui.Canvas;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Font;
import com.nttdocomo.ui.Graphics;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author tsuchimoto
 */
public class View extends Canvas implements Runnable
{
    private Thread m_Thread;
    
    private static final int STATE_TITLE=0;
    private static final int STATE_GAME=1;
    private static final int STATE_GAMEOVER=2;

    private int m_State=STATE_TITLE;

    private static final float GRAV=0.8f;
    private static final int MAXS=60;
    private int m_Score;
    private Shape m_Player=new Shape();
    private Shape m_Bullet=new Shape();
    private float m_PassTime;

    private Vector m_Shapes=new Vector();


    public View()
    {
        m_Thread=new Thread(this);
        m_Thread.start();
        setBackground(Graphics.getColorOfName(Graphics.BLACK));
    }

    public void paint(Graphics aGraphics)
    {
        aGraphics.lock();
        aGraphics.clearRect(0, 0, getWidth(), getHeight());
        switch(m_State)
        {
            case STATE_TITLE:
            {
                aGraphics.setColor(Graphics.getColorOfName(Graphics.WHITE));
                Font aFont = Font.getFont(Font.SIZE_LARGE);
                aGraphics.setFont(aFont);
                aGraphics.drawString("PRESS SELECT TO START GAME", 20, getHeight()/2);
            }
                break;
            case STATE_GAME:
            case STATE_GAMEOVER:
            {
                for (int i=0;i<m_Shapes.size();i++)
                {
                    ((Shape)m_Shapes.elementAt(i)).paint(aGraphics);
                }
                aGraphics.setColor(Graphics.getColorOfName(Graphics.WHITE));
                Font aFont = Font.getFont(Font.SIZE_MEDIUM);
                aGraphics.setFont(aFont);
                aGraphics.drawString("Score: " + m_Score, 0, 20);

                if (m_State == STATE_GAMEOVER)
                {
                    aGraphics.setColor(Graphics.getColorOfName(Graphics.WHITE));
                    aFont = Font.getFont(Font.SIZE_LARGE);
                    aGraphics.setFont(aFont);
                    String message = "Your score is: " + m_Score;
                    int width = aFont.getBBoxWidth(message);
                    aGraphics.drawString(message, getWidth()/2 - width, getHeight()/2);
                }
            }
                break;
        }
        aGraphics.unlock(true);
    }

    public void run()
    {
        m_Player.m_Blush=Shape.RECTANGLE;
        m_Player.m_Width=20;
        m_Player.m_Height=20;
        m_Player.m_Color=Graphics.getColorOfName(Graphics.GREEN);
        m_Shapes.addElement(m_Player);

        m_Bullet.m_Blush=Shape.RECTANGLE;
        m_Bullet.m_Width=6;
        m_Bullet.m_Height=30;
        m_Bullet.m_Color=Graphics.getColorOfName(Graphics.BLUE);
        m_Shapes.addElement(m_Bullet);

        while(true)
        {
            m_State=STATE_TITLE;
            while ((getKeypadState() & (1 << Display.KEY_SELECT)) == 0)
            {
                repaint();
                sleep();
            }

            m_State=STATE_GAME;
            boolean flaglive=true;
            m_Score=0;
            m_Player.m_X=getWidth()/2;
            m_Player.m_Y=getHeight()-50;
            m_Bullet.m_Show=false;
            while(flaglive)
                GameLoop:
                {
                    playerMove();

                    if (m_PassTime > 4.0f)
                    {
                        ballAdd();
                        m_PassTime=0;
                    }

                    if (m_Bullet.m_Show)
                    {
                        bulletMove();
                    }

                    m_PassTime += 0.02;
                    repaint();
                    sleep();
                }

            m_State=STATE_GAMEOVER;
            while ((getKeypadState() & (1 << Display.KEY_SELECT)) == 0)
            {
                repaint();
                sleep();
            }

        }
    }

    private void sleep()
    {
        try
        {
            Thread.sleep(20);
        } catch (Exception anException)
        {
        }
    }

    private void ballAdd()
    {
    }

    private void bulletMove()
    {
        m_Bullet.m_Y -= 12;
        if (m_Bullet.m_Y < 0)
        {
            m_Bullet.m_Show=false;
        }
    }

    private void playerMove()
    {
        if ((getKeypadState() & (1 << Display.KEY_LEFT)) != 0)
        {
            m_Player.m_X -= 5;
        }
        if ((getKeypadState() & (1 << Display.KEY_RIGHT)) != 0)
        {
            m_Player.m_X += 5;
        }
        if ((getKeypadState() & (1 << Display.KEY_SELECT)) != 0)
        {
            m_Bullet.m_Show=true;
            m_Bullet.m_X=m_Player.m_X;
            m_Bullet.m_Y=m_Player.m_Y;
        }
    }

}
